<?php

namespace App\Controller;

use App\Service\CosService;
use App\Service\MyObjectService;
use Hyperf\Di\Annotation\Inject;

class MyObjectController extends AbstractController
{
    /**
     * @Inject
     */
    protected MyObjectService $myObjectService;

    /**
     * @Inject
     */
    protected CosService $cosService;

    public function fetchObjects()
    {
        $page = $this->request->input('page', 1);
        $list = $this->myObjectService->fetchObjects((int) $page);
        return $this->success([
            'list' => $list,
        ]);
    }

    public function createObject()
    {
        $name = $this->request->input('name');
        $url = $this->request->input('url');
        $brief = $this->request->input('brief', '');
        $address = $this->request->input('address', '');;
        $this->myObjectService->createObject($name, $url, $address, $brief);
        return $this->success();
    }

    public function searchObjects()
    {
        $search = $this->request->input('search');
        $res = $this->myObjectService->searchObjects($search);
        return $this->success([
            'list' => $res,
        ]);
    }

    public function updateObject()
    {
        $id = (int) $this->request->input('id');
        $name = $this->request->input('name');
        $url = $this->request->input('url');
        $brief = $this->request->input('brief');
        $address = $this->request->input('address');
        $this->myObjectService->updateObject($id, $name, $url, $address, $brief);
        return $this->success();
    }

    public function removeObject()
    {
        $id = (int) $this->request->input('id');
        $this->myObjectService->removeObject($id);
        return $this->success();
    }

    public function fetchUploadKey()
    {
        $res = $this->cosService->getUploadObjectKey();
        return $this->success($res);
    }
}